package ThreadPool2;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory  implements ThreadFactory {
    private static final AtomicInteger threadIndex = new AtomicInteger(0);
    private  final  String baseName;
    private  final  boolean daemon;

    public NamedThreadFactory(String baseName) {
       this(baseName , true);
    }
    public NamedThreadFactory(String baseName, boolean daemon) {
        this.baseName = baseName;
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
       Thread thread = new Thread(r , this.baseName + "-" + threadIndex.getAndIncrement());
       thread.setDaemon(this.daemon);
       return thread;
    }
}
